﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMail.Common.Interfaces.Data;

namespace AMail.Modules.Data.Actions
{
  class MoveFoldersAction : IMailAction
  {
    private List<IMailFolder> folders;
    private IMailFolder previousParentFolder;

    public MoveFoldersAction(IEnumerable<IMailFolder> folders, IMailFolder previousParentFolder)
    {
      this.previousParentFolder = previousParentFolder;
      this.folders = new List<IMailFolder>(folders);
    }

    public MoveFoldersAction(IMailFolder folder, IMailFolder previousParentFolder)
    {
      this.previousParentFolder = previousParentFolder;
      this.folders = new List<IMailFolder> { folder };
    }


    #region IMailAction Members

    public void Commit(IMailArchiver archiver)
    {
    }

    public void Undo()
    {
      foreach (var folder in this.folders)
        folder.ParentFolder = this.previousParentFolder;
    }

    #endregion

    public override string ToString()
    {
      if (this.folders.Count == 1)
        return string.Format("Moved folder '{0}' from '{1}' to '{2}'", 
          this.folders[0].Name, this.previousParentFolder.Name, this.folders[0].ParentFolder.Name);
      else
        return string.Format("Moved {0} folders from '{1}' to '{2}'",
          this.folders.Count, this.previousParentFolder.Name, this.folders[0].ParentFolder.Name);
    }
  }
}
